package com.example.project.controller.project;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.core.text.Convert;
import com.example.project.entity.Issue;
import com.example.project.entity.IssueOperationRecord;
import com.example.project.entity.Project;
import com.example.project.enums.BusinessType;
import com.example.project.service.IssueOperationRecordService;
import com.example.project.service.IssueService;
import com.example.project.service.ProjectService;
import com.example.project.utils.poi.ExcelUtil;

/**
 * 问题操作记录Controller
 * 
 * @author system
 * @date 2025-11-21
 */
@Controller
@RequestMapping("/project/issueOperation")
public class IssueOperationRecordController extends BaseController {
    
    private String prefix = "pm/issueOperation";

    @Autowired
    private IssueOperationRecordService issueOperationRecordService;
    
    @Autowired
    private IssueService issueService;
    
    @Autowired
    private ProjectService projectService;

    @RequiresPermissions("project:issueOperation:view")
    @GetMapping()
    public String issueOperation() {
        return prefix + "/issueOperation";
    }

    /**
     * 查询问题操作记录列表
     */
    @RequiresPermissions("project:issueOperation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IssueOperationRecord issueOperationRecord) {
        startPage();
        List<IssueOperationRecord> list = issueOperationRecordService.selectIssueOperationRecordList(issueOperationRecord);
        list.forEach(info->{
        	Long issueId = info.getIssueId();
        	Issue issue = issueService.selectIssuesById(issueId);
        	info.setIssue(issue);
        	Long projectId = info.getProjectId()	;
        	Project project = projectService.selectProjectById(projectId);
        	info.setProject(project);
        });
        return getDataTable(list);
    }

    /**
     * 导出问题操作记录列表
     */
    @RequiresPermissions("project:issueOperation:export")
    @Log(title = "问题操作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IssueOperationRecord issueOperationRecord) {
        List<IssueOperationRecord> list = issueOperationRecordService.selectIssueOperationRecordList(issueOperationRecord);
        ExcelUtil<IssueOperationRecord> util = new ExcelUtil<IssueOperationRecord>(IssueOperationRecord.class);
        return util.exportExcel(list, "问题操作记录数据");
    }

    /**
     * 新增问题操作记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存问题操作记录
     */
    @RequiresPermissions("project:issueOperation:add")
    @Log(title = "问题操作记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IssueOperationRecord issueOperationRecord) {
        return toAjax(issueOperationRecordService.insertIssueOperationRecord(issueOperationRecord));
    }

    /**
     * 修改问题操作记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        IssueOperationRecord issueOperationRecord = issueOperationRecordService.selectIssueOperationRecordById(id);
        mmap.put("issueOperationRecord", issueOperationRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存问题操作记录
     */
    @RequiresPermissions("project:issueOperation:edit")
    @Log(title = "问题操作记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IssueOperationRecord issueOperationRecord) {
        return toAjax(issueOperationRecordService.updateIssueOperationRecord(issueOperationRecord));
    }

    /**
     * 删除问题操作记录
     */
    @RequiresPermissions("project:issueOperation:remove")
    @Log(title = "问题操作记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(issueOperationRecordService.deleteIssueOperationRecordByIds(Convert.toLongArray(ids)));
    }
    
    /**
     * 查看问题操作记录
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap) {
        IssueOperationRecord issueOperationRecord = issueOperationRecordService.selectIssueOperationRecordById(id);
        mmap.put("issueOperationRecord", issueOperationRecord);
        return prefix + "/view";
    }
    
    /**
     * 问题操作记录列表页面（根据问题ID）
     */
    @GetMapping("/issue/{issueId}")
    public String issueRecordList(@PathVariable("issueId") Long issueId, ModelMap mmap) {
        // 获取问题信息
        Issue issue = issueService.selectIssuesById(issueId);
        if (issue != null) {
            mmap.put("issue", issue);
            // 获取项目信息
            if (issue.getProjectId() != null) {
                Project project = projectService.selectProjectById(issue.getProjectId());
                mmap.put("project", project);
            }
        }
        return prefix + "/issueRecordList";
    }
    
    /**
     * 查询问题操作记录列表（根据问题ID）
     */
    @PostMapping("/issue/list/{issueId}")
    @ResponseBody
    public TableDataInfo issueList(@PathVariable("issueId") Long issueId) {
        startPage();
        List<IssueOperationRecord> list = issueOperationRecordService.selectRecordsByIssueId(issueId);
        return getDataTable(list);
    }
    
    /**
     * 项目操作记录列表页面（根据项目ID）
     */
    @GetMapping("/project/{projectId}")
    public String projectRecordList(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        // 获取项目信息
        Project project = projectService.selectProjectById(projectId);
        mmap.put("project", project);
        return prefix + "/projectRecordList";
    }
    
    /**
     * 查询项目操作记录列表（根据项目ID）
     */
    @PostMapping("/project/list/{projectId}")
    @ResponseBody
    public TableDataInfo projectList(@PathVariable("projectId") Long projectId) {
        startPage();
        List<IssueOperationRecord> list = issueOperationRecordService.selectRecordsByProjectId(projectId);
        return getDataTable(list);
    }
}